package br.ufmg.ubicomp.droidguide.scheduler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.w3c.dom.NodeList;

import br.ufmg.ubicomp.droidguide.common.EntityProperties;
import br.ufmg.ubicomp.droidguide.common.Location;
import br.ufmg.ubicomp.droidguide.eventservice.DroidGuideAbstractService;
import br.ufmg.ubicomp.droidguide.utils.RequestUtil;
import br.ufmg.ubicomp.droidguide.utils.XmlParseUtil;


public class SchedulerService extends DroidGuideAbstractService{

	private static SchedulerService theService;
	
	private SchedulerService() {
	}
	
	public static SchedulerService getInstance()
	{
		if (theService == null)
		{
			theService = new SchedulerService();
		}
		return theService;		
	}
	
	public Collection<DroidGuideActivity> getActivitiesForUser(String dgKey) throws Exception
	{
		operation = "activity/process?";
		params = "operation=getForUser&droidGuideUser_key="+dgKey;
		String url = getServerUrl() + operation + params;
		
		NodeList node = RequestUtil.executeUrlDOM(url);
		
		Collection<EntityProperties> list = XmlParseUtil.getEntities(node);
		
		// expected a list of kinds DroidGuideActivity and Landmark
		Collection<DroidGuideActivity> activities = new ArrayList<DroidGuideActivity>();
		DroidGuideActivity activity = null;
		for (EntityProperties ep: list)
		{
			if (ep.getKind().equals("UserActivity"))
			{
				activity = new DroidGuideActivity();
				activity.setUserActivityKey(ep.getValue("key"));
			} else if (ep.getKind().equals("Landmark"))
			{
				activity.setDescription(ep.getValue("description"));
				Location loc = new Location();
				loc.setLatitude(Double.parseDouble(ep.getValue("lat")));
				loc.setLongitude(Double.parseDouble(ep.getValue("lon")));
				activity.setLocation(loc);
				activity.setName(ep.getValue("title"));
			} else if (ep.getKind().equals("DroidGuideActivity"))
			{
				activity.setKey(ep.getValue("key"));
				activity.setLandmarkId(ep.getValue("landmark_id"));
				activity.setSignature(ep.getValue("signature"));
				activities.add(activity);
			}
		}
		
		return activities;
	}
	
	public Collection<DroidGuideActivity> listActivities(String dgKey) throws Exception
	{
		operation = "activity/process?";
		params = "operation=list";
		String url = getServerUrl() + operation + params;
		
		NodeList node = RequestUtil.executeUrlDOM(url);
		
		Collection<EntityProperties> list = XmlParseUtil.getEntities(node);
		// expected a list of kinds DroidGuideActivity and Landmark
		Collection<DroidGuideActivity> activities = new ArrayList<DroidGuideActivity>();
		DroidGuideActivity activity = null;
		for (EntityProperties ep: list)
		{
			activity = new DroidGuideActivity();
			if (ep.getKind().equals("UserActivity"))
			{
				activity.setUserActivityKey(ep.getValue("key"));
			} else if (ep.getKind().equals("Landmark"))
			{
				activity.setDescription(ep.getValue("description"));
				Location loc = new Location();
				loc.setLatitude(Double.parseDouble(ep.getValue("lat")));
				loc.setLongitude(Double.parseDouble(ep.getValue("lon")));
				activity.setLocation(loc);
				activity.setName(ep.getValue("title"));
			} else if (ep.getKind().equals("DroidGuideActivity"))
			{
				activity.setKey(ep.getValue("key"));
				activity.setLandmarkId(ep.getValue("landmark_id"));
				activity.setSignature(ep.getValue("signature"));
				activities.add(activity);
			}
		}
		
		return activities;
	}

	public int subscribeToActivities(List<DroidGuideActivity> activitiesToSubscribe, String dgKey) throws IOException, Exception {

		operation = "activity/process?";
		params = "operation=subscribeActivity&droidGuideUser_key=" + dgKey;
		String url = getServerUrl() + operation + params;
		int num = 0;
		for (DroidGuideActivity activity: activitiesToSubscribe)
		{
			params = params + "&userActivity_key="+activity.getUserActivityKey();
			NodeList node = RequestUtil.executeUrlDOM(url);
			Collection<EntityProperties> list = XmlParseUtil.getEntities(node);
			System.out.println(list.size());
			num ++;
		}
		
		return num;
	}

	public int unsubscribeToActivities(
			List<DroidGuideActivity> activitiesToUnSubscribe, String dgKey) throws IOException, Exception {
		operation = "activity/process?";
		params = "operation=unsubscribeActivity&droidGuideUser_key=" + dgKey;
		String url = getServerUrl() + operation + params;
		int num = 0;
		for (DroidGuideActivity activity: activitiesToUnSubscribe)
		{
			params = params + "&userActivity_key="+activity.getUserActivityKey();
			NodeList node = RequestUtil.executeUrlDOM(url);
			Collection<EntityProperties> list = XmlParseUtil.getEntities(node);
			System.out.println(list.size());
			num ++;
		}
		
		return num;
	}
}
